## Table 1:Firm and Employee Donations and Lobbying of Specific Politicians, 
# First-Time Donation in 3 Years: ---

## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Adjust DATA_DIR to where Data is is located
# Step 3: Adjust FIG_DIR to where Output folder is
# Step 4: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())

# load packages
library(dplyr)
library(PanelMatch)
library(ggplot2)
library(readr)
options(scipen=999)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"
## MAIN_DIR <- "~/Dropbox (MIT)/projects/campaign-lobby-paper/replication_package"

# if necessary, install PanelMatch 2.0.1 version
# install.packages(paste0(MAIN_DIR, "/PanelMatch_2.0.1.tar.gz"), repos = NULL, type="source")


# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/", sep = "")

# set output folder
FIG_DIR <- paste(MAIN_DIR, "/output/", sep = "")

# file to be read in
#file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.csv") # file to be input
file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.rds") # file to be input



# load lobbying and donations data
d <- readRDS(file_in) %>%
  select(gvkey, govtrack_id, don_any_bin, year)

# d <- read_csv(file_in, 
#               col_select = c("gvkey", "govtrack_id", "don_any_bin", "year"))  
gc()
firm_ids <- c("006774", "004503", "006730")
treat_plot <- d %>% filter(gvkey %in% firm_ids & year %in% c(2007:2018)) %>% as.data.frame()
treat_plot$year <- as.integer(treat_plot$year)

# Lockheed Martin Corp.
pdf(paste0(FIG_DIR, "figure_2_006774.pdf"), height = 6, width = 5)
par(font.lab = 2, cex.lab = 3, cex.axis = 2, cex.main = 5, mar = c(6,6,5,2), las=1, mfrow=c(1,3))
DisplayTreatment(unit.id = "govtrack_id",
                 time.id = "year",
                 legend.position = "none",
                 xlab = "Year",
                 ylab = "Firm-Politician Pair",
                 treatment = "don_any_bin",
                 data = treat_plot[treat_plot$gvkey == "006774",],
                 dense.plot = T,
                 x.size = 14,
                 y.size = 14,
                 hide.y.tick.label = TRUE,
                 title = "Lockheed Martin Corp.") +
  theme(axis.title.x = element_text(color = "black", size = 20, face = "plain"),
        axis.title.y = element_text(color = "black", size = 20, face = "plain"),
        title = element_text(color = "black", size = 20, face = "bold"),
        axis.text.x = element_text(color = "black", size = 12, face = "plain", vjust = 1.6, hjust = 1.5, angle = 45))
dev.off()

# Exxon Mobile Corp.
pdf(paste0(FIG_DIR, "figure_2_004503.pdf"), height = 6, width = 5)
par(font.lab = 2, cex.lab = 3, cex.axis = 2, cex.main = 5, mar = c(6,6,5,2), las=1, mfrow=c(1,3))
DisplayTreatment(unit.id = "govtrack_id",
                 time.id = "year",
                 legend.position = "none",
                 xlab = "Year",
                 ylab = "Firm-Politician Pair",
                 treatment = "don_any_bin",
                 data = treat_plot[treat_plot$gvkey == "004503",],
                 hide.y.tick.label = TRUE,
                 dense.plot = T,
                 x.size = 14,
                 y.size = 14,
                 title = "Exxon Mobile Corp.") +
  theme(axis.title.x = element_text(color = "black", size = 20, face = "plain"),
        axis.title.y = element_text(color = "black", size = 20, face = "plain"),
        title = element_text(color = "black", size = 20, face = "bold"),
        axis.text.x = element_text(color = "black", size = 12, face = "plain", vjust = 1.6, hjust = 1.5, angle = 45))
dev.off()

# Eli Lilly & Co.
pdf(paste0(FIG_DIR, "figure_2_006730.pdf"), height = 6, width = 5)
par(font.lab = 2, cex.lab = 3, cex.axis = 2, cex.main = 5, mar = c(6,6,5,2), las=1, mfrow=c(1,3))
DisplayTreatment(unit.id = "govtrack_id",
                 time.id = "year",
                 legend.position = "none",
                 xlab = "Year",
                 ylab = "Firm-Politician Pair",
                 treatment = "don_any_bin",
                 data = treat_plot[treat_plot$gvkey == "006730",],
                 hide.y.tick.label = TRUE,
                 dense.plot = T,
                 x.size = 14,
                 y.size = 14,
                 title = "Eli Lilly & Co.") +
  theme(axis.title.x = element_text(color = "black", size = 20, face = "plain"),
        axis.title.y = element_text(color = "black", size = 20, face = "plain"),
        title = element_text(color = "black", size = 20, face = "bold"),
        axis.text.x = element_text(color = "black", size = 12, face = "plain", vjust = 1.6, hjust = 1.5, angle = 45))
dev.off()
